package com.sinosoft.web.controller.manage;


import com.alibaba.fastjson2.JSON;
import com.sinosoft.common.core.domain.AjaxResult;
import com.sinosoft.common.manage.service.*;
import com.sinosoft.common.manage.vo.*;
import com.sinosoft.common.utils.StringUtils;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.sinosoft.common.core.domain.AjaxResult.error;
import static com.sinosoft.common.core.domain.AjaxResult.success;

/**
 * @author Yeung
 * @desc 智能核保控制器
 * @date 2024-04-24  11:48
 * @Version 1.0.0
 */
@RestController
@Slf4j
@RequestMapping("/intelligentUnderwriting")
public class IntelligentUnderwritingController {
    @Resource
    private GoodsUnderDiseaseInfoService goodsUnderDiseaseInfoService;
    @Resource
    private GoodsUnderIssueAnswerService goodsUnderIssueAnswerService;
    @Resource
    private GoodsUnderVerdictInfoService goodsUnderVerdictInfoService;

    @GetMapping(value = "/queryDiseaseAll")
    @ApiOperation(value = "病种信息列表查询接口", httpMethod = "GET")
    public AjaxResult queryDiseaseAll() {
        try {
            List<DiseaseVO> diseaseVOList = goodsUnderDiseaseInfoService.queryDiseaseAll();
            log.info("com.sinosoft.web.controller.manage.IntelligentUnderwritingController.queryDiseaseAll 返回={}", JSON.toJSONString(diseaseVOList));
            if (!diseaseVOList.isEmpty()) {
                return success(diseaseVOList);
            } else {
                return error("未查询到产品关联病种信息");
            }
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    @PostMapping(value = "/queryByIssueId")
    @ApiOperation(value = "问题答案关联信息列表查询接口", httpMethod = "POST")
    public AjaxResult queryByIssueId(@RequestBody List<GoodsUnderDiseaseInfoVO> goodsUnderDiseaseInfoVos) {
        log.info("com.sinosoft.web.controller.manage.IntelligentUnderwritingController.queryByIssueId 入参={}", JSON.toJSONString(goodsUnderDiseaseInfoVos));
        try {
            //封装参数
            List<GoodsUnderDiseaseInfoVO> goodsUnderDiseaseInfoVOList = new ArrayList<>();
            for (GoodsUnderDiseaseInfoVO diseaseinfoVOGoodsUnder : goodsUnderDiseaseInfoVos) {
                String issueId = diseaseinfoVOGoodsUnder.getIssueId();
                String sonDiseaseName = diseaseinfoVOGoodsUnder.getSonDiseaseName();
                if (issueId.contains("-")) {
                    String[] strings = issueId.split("-");
                    for (String string : strings) {
                        GoodsUnderDiseaseInfoVO diseaseInfo = new GoodsUnderDiseaseInfoVO();
                        diseaseInfo.setIssueId(string);
                        diseaseInfo.setSonDiseaseName(sonDiseaseName);
                        goodsUnderDiseaseInfoVOList.add(diseaseInfo);
                    }
                } else {
                    GoodsUnderDiseaseInfoVO diseaseInfo = new GoodsUnderDiseaseInfoVO();
                    diseaseInfo.setIssueId(issueId);
                    diseaseInfo.setSonDiseaseName(sonDiseaseName);
                    goodsUnderDiseaseInfoVOList.add(diseaseInfo);
                }
            }
            List<IssueInfoAnswerVO> issueInfoAnswerVOList = goodsUnderIssueAnswerService.queryByIssueId(goodsUnderDiseaseInfoVOList);
            log.info("com.sinosoft.web.controller.manage.IntelligentUnderwritingController.queryByIssueId 返回={}", JSON.toJSONString(issueInfoAnswerVOList));
            if (!issueInfoAnswerVOList.isEmpty()) {
                return success(issueInfoAnswerVOList);
            } else {
                return error("未查询到问题答案关联信息");
            }
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    @PostMapping(value = "/queryByVerdict")
    @ApiOperation(value = "智能核保接口", httpMethod = "POST")
    public AjaxResult queryByVerdict(@RequestBody VerdictVO verdictVO) {
        log.info("com.sinosoft.web.controller.manage.IntelligentUnderwritingController.queryByVerdict 入参={}", JSON.toJSONString(verdictVO));
        Map<String,String> map = new HashMap<>();
        try {
            if (CollectionUtils.isEmpty(verdictVO.getDiseaseIds())) {
                List<String> diseaseIds = new ArrayList<>();
                diseaseIds.add(null);
                verdictVO.setDiseaseIds(diseaseIds);
            }
            if (CollectionUtils.isEmpty(verdictVO.getIssueIds())) {
                List<String> issueIds = new ArrayList<>();
                issueIds.add(null);
                verdictVO.setIssueIds(issueIds);
            }
            if (CollectionUtils.isEmpty(verdictVO.getAnswerIds())) {
                List<String> answerIds = new ArrayList<>();
                answerIds.add(null);
                verdictVO.setAnswerIds(answerIds);
            }
            List<GoodsUnderVerdictInfoVO> goodsUnderVerdictInfoVOList = goodsUnderVerdictInfoService.queryByVerdict(verdictVO);
            log.info("com.sinosoft.web.controller.manage.IntelligentUnderwritingController.queryByVerdict 返回={}", JSON.toJSONString(goodsUnderVerdictInfoVOList));
            if (!goodsUnderVerdictInfoVOList.isEmpty()) {
                List<String> explainTexts = new ArrayList<>();
                StringBuilder stringBuilder = new StringBuilder();
                for (GoodsUnderVerdictInfoVO goodsUnderVerdictInfoVO : goodsUnderVerdictInfoVOList) {
                    String explainText = goodsUnderVerdictInfoVO.getExplainText();
                    String conclusion = goodsUnderVerdictInfoVO.getConclusion();
                    String verdict = goodsUnderVerdictInfoVO.getVerdict();
                    if (StringUtils.isNotEmpty(verdict) && "03".equals(verdict)) {
                        explainTexts.add(explainText);
                    }
                    if (StringUtils.isNotEmpty(conclusion) && "03".equals(conclusion)) {
                        explainTexts.add(explainText);
                    }
                }
                List<String> stringList = explainTexts.stream().distinct().collect(Collectors.toList());
                for (String s : stringList) {
                    stringBuilder.append(s).append('\n');
                }
                List<String> conclusions = new ArrayList<>();
                List<String> verdicts = new ArrayList<>();
                for (GoodsUnderVerdictInfoVO goodsUnderVerdictInfoVO : goodsUnderVerdictInfoVOList) {
                    String conclusion = goodsUnderVerdictInfoVO.getConclusion();
                    String verdict = goodsUnderVerdictInfoVO.getVerdict();
                    conclusions.add(conclusion);
                    verdicts.add(verdict);
                }
                map.put("conclusion","标体承保");
                map.put("verdict","标体承保");
                map.put("explainText",stringBuilder.toString());

                if (conclusions.contains("03")){
                    map.put("conclusion","除外承保");
                }
                if (conclusions.contains("04")){
                    map.put("conclusion","人核");
                }
                if (conclusions.contains("01")){
                    map.put("conclusion","拒保");
                }
                if (verdicts.contains("03")){
                    map.put("verdict","除外承保");
                }
                if (verdicts.contains("04")){
                    map.put("verdict","人核");
                }
                if (verdicts.contains("01")){
                    map.put("verdict","拒保");
                }
                return success(map);
            } else {
                return error("未查询到核保结论信息");
            }
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

}
